home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * qsort.c
- *
- * Copyright (c) 1989 Symantec Corporation. All rights reserved.
- *
- */
-
- #include "stdlib.h"
-
- static char *qBase;
- static size_t qSize;
- static int (*qCompare)();
-
- static int std_compare(size_t, size_t);
- static void std_swap(size_t, size_t);
-
- static void qsort1(size_t, size_t);
- static int (*q1Compare)();
- static void (*q1Swap)();
-
-
- /* ---------- standard quicksort ---------- */
-
-
- void qsort(base, n, size, compare)
- void *base;
- size_t n, size;
- int (*compare)();
- {
- qBase = base;
- /* qSize = (size + 1) & ~1; why is this - pvh 8/12/89 ???? */
- qSize = size; /* changed to use without complement - pvh 8/12/89 */
- qCompare = compare;
- _qsort(n, std_compare, std_swap);
- }
-
-
- static int
- std_compare(i, j)
- size_t i, j;
- {
- return((*qCompare)(qBase + i * qSize, qBase + j * qSize));
- }
-
-
- static void
- std_swap(i, j)
- size_t i, j;
- {
- register void *p = qBase + i * qSize;
- register void *q = qBase + j * qSize;
-
- asm {
- move.l qSize,d0
- @1 move.b (p)+,d1
- eor.b d1,(q)+
- subq.l #1,d0
- bne.s @1
- }
- asm {
- move.l qSize,d0
- @2 move.b -(q),d1
- eor.b d1,-(p)
- subq.l #1,d0
- bne.s @2
- }
- asm {
- move.l qSize,d0
- @3 move.b (p)+,d1
- eor.b d1,(q)+
- subq.l #1,d0
- bne.s @3
- }
- }
-
-
- /* ---------- general quicksort ---------- */
-
-
- void
- _qsort(n, compare, swap)
- size_t n;
- int (*compare)();
- void (*swap)();
- {
- q1Compare = compare;
- q1Swap = swap;
- qsort1(0, n);
- }
-
-
- /*
- * sort elements "first" through "last"-1
- *
- */
-
- static void
- qsort1(first, last)
- size_t first, last;
- {
- static size_t i; /* "static" to save stack space */
- size_t j;
-
- while (last - first > 1) {
- i = first;
- j = last;
- for (;;) {
- while (++i < last && (*q1Compare)(i, first) < 0)
- ;
- while (--j > first && (*q1Compare)(j, first) > 0)
- ;
- if (i >= j)
- break;
- (*q1Swap)(i, j);
- }
- if (j == first) {
- ++first;
- continue;
- }
- (*q1Swap)(first, j);
- if (j - first < last - (j + 1)) {
- qsort1(first, j);
- first = j + 1; /* qsort1(j + 1, last); */
- }
- else {
- qsort1(j + 1, last);
- last = j; /* qsort1(first, j); */
- }
- }
- }
-